package com.zkx.mvp.view;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.zkx.mvp.R;
import com.zkx.mvp.utils.StringUtil;
import com.zkx.mvp.view.wight.LoadingDialog;

/**
 * Author：zhangkx on 2017/6/28 16:40
 * Email：zkx9025@163.com
 * fragment的基类
 */

public abstract class BaseFragment extends ProgressFragment {
    /**
     * 当前操作上下下文
     */
    protected Context context = null;
    private View rootView;
    protected LoadingDialog mLoadingDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        mLoadingDialog = new LoadingDialog(context, R.style.dialog);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        rootView = initView(LayoutInflater.from(context));
        setContentView(rootView);
        setEmptyText("暂无数据");
        setProgressText(getResources().getString(R.string.loading));
        initData();
    }
    /**
     * Fragment视图缓加载.
     */
    protected abstract View initView(LayoutInflater inflater);
    /**
     * Fragment数据的缓加载.
     */
    protected abstract void initData();

    /**
     * 设置默认的titlebar
     * @param title 标题名称
     * @param onClickListener 左边返回箭头的点击监听
     * @param idRight 右侧图标的 resId
     * @param onRightClick 右侧点击监听
     */
    public void setActionBarDefault(String title, View.OnClickListener onClickListener, Integer idRight,
                                    View.OnClickListener onRightClick) {
        View rl_back = rootView.findViewById(R.id.activity_back);
        TextView tv_title = (TextView) rootView.findViewById(R.id.activity_title);
        tv_title.setText(StringUtil.parseEmpty(title));
        if (onClickListener == null) {
            rl_back.setVisibility(View.GONE);
        } else {
            rl_back.setVisibility(View.VISIBLE);
            rl_back.setOnClickListener(onClickListener);
        }
        if (idRight != null) {
            Button iv_title_right = (Button) rootView.findViewById(R.id.iv_title_right);
            iv_title_right.setVisibility(View.VISIBLE);
            iv_title_right.setBackgroundResource(idRight);
            View action_bar_right = rootView.findViewById(R.id.action_bar_right);
            if (onRightClick != null) {
                action_bar_right.setOnClickListener(onRightClick);
            }
        }
    }

    /**
     * 设置返回箭头显示状态
     * @param visable 取值 VISABLE GONE INVISABLE
     */
    public void setBackVisibility(int visable){
        View rl_back = rootView.findViewById(R.id.activity_back);
        rl_back.setVisibility(visable);
    }

    /**
     * 设置右边标题点击事件
     *
     * @param onRightClick
     */
    public void setActionBarRight(String title, String text, OnClickListener onClickListener, int textColor,
                                  OnClickListener onRightClick) {
        TextView iv_title_right = (TextView) rootView.findViewById(R.id.iv_title_right);
        View rl_back = rootView.findViewById(R.id.activity_back);
        if (onClickListener == null) {
            rl_back.setVisibility(View.GONE);
        } else {
            rl_back.setVisibility(View.VISIBLE);
            rl_back.setOnClickListener(onClickListener);
        }
        iv_title_right.setVisibility(View.VISIBLE);
        iv_title_right.setText(StringUtil.parseEmpty(text));
		/* iv_title_right.setTextColor(textColor); */
        TextView tv_title = (TextView) rootView.findViewById(R.id.activity_title);
        tv_title.setText(StringUtil.parseEmpty(title));
        View action_bar_right = rootView.findViewById(R.id.action_bar_right);
        if (onRightClick != null) {
            action_bar_right.setOnClickListener(onRightClick);
        }
    }
    /**
     * 设置右边标题点击事件
     *
     * @param onRightClick
     */
    public void setActionBarRightOnlic(String title, String text, OnClickListener onClickListener,
                                       OnClickListener onRightClick) {
        TextView iv_title_right = (TextView) rootView.findViewById(R.id.iv_title_right);
        View rl_back = rootView.findViewById(R.id.activity_back);
        if (onClickListener == null) {
            rl_back.setVisibility(View.GONE);
        } else {
            rl_back.setVisibility(View.VISIBLE);
            rl_back.setOnClickListener(onClickListener);
        }
        iv_title_right.setVisibility(View.VISIBLE);
        iv_title_right.setText(StringUtil.parseEmpty(text));
        TextView tv_title = (TextView) rootView.findViewById(R.id.activity_title);
        tv_title.setText(StringUtil.parseEmpty(title));
        if (onRightClick != null) {
            iv_title_right.setOnClickListener(onRightClick);
        }
    }
    /**
     * 设置title
     *
     * @param title
     */
    public void setTitle(String title) {
        TextView tv_title = (TextView) rootView.findViewById(R.id.activity_title);
        tv_title.setText(StringUtil.parseEmpty(title));
    }
    /**
     * gettitle
     */
    public String getTitle() {
        TextView tv_title = (TextView) rootView.findViewById(R.id.activity_title);
        return tv_title.getText().toString();
    }

}
